﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Media.Imaging;
using Microsoft.Phone.Shell;

namespace ReadManga.GUI
{
    public partial class ViewDetails : PhoneApplicationPage
    {
        WebClient client = new WebClient();
        private Popup popup;
        string Url="";
        string ImageUrl="";
        string Descript="";

        public ViewDetails()
        {
            this.popup = new Popup();
            this.popup.Child = new LoadingUI();
            this.popup.IsOpen = true;
            InitializeComponent();           
        }
        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);

            NavigationContext.QueryString.TryGetValue("Url", out Url);
            NavigationContext.QueryString.TryGetValue("UrlImage", out ImageUrl);
            NavigationContext.QueryString.TryGetValue("Description", out Descript);

            if (ImageUrl != "")
            {
                Thumbnail.Source = new BitmapImage(new Uri(ImageUrl));    
            }
            if (Descript != null)
            {
                Description.Text = Descript;
            }
            if (Url != "")
            {
                client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(client_DownloadProgressChanged);
                client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(client_DownloadStringCompleted);
                client.DownloadStringAsync(new Uri(Url), "blogtruyen");
            }
        }
        void client_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
        {
            if (e.UserState as string == "blogtruyen")
            {
            }
        }
        void client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            if (e.Error == null && !e.Cancelled)
            {
                title.Text = Utils.GetStringBetweenText(e.Result, "<h1>", "</h1>");

                GetThumbnailImage(e.Result);

                LoadList(Utils.GetStringBetweenText(e.Result, "<div class=\"danhsach\">",e.Result.IndexOf("<a name=\"formcomment\">")>0?"<a name=\"formcomment\">":"<div class=\"paging-div\""));
               
                this.popup.IsOpen = false;
            }
        }
        void GetThumbnailImage(string content)
        {
            if(ImageUrl=="")
            {
                ImageUrl=Utils.GetStringBetweenText(content,"src=\"","\"");
                Thumbnail.Source = new BitmapImage(new Uri(ImageUrl));   
            }
            if(Descript=="")
            {
                Description.Text = Utils.strip_tags(Utils.GetStringBetweenText(content, "<span class=\"mota\">", "</span>").Trim());            }
        }
        void LoadList(string content)
        {
            listChap.ItemsSource = null;
            List<Class.ChapItem> Chaplist = new List<Class.ChapItem>();
            string CheckContent = content;
            string line = "";
            int index = 0;
            line = Utils.GetStringBetweenText(CheckContent, "<a href=", "/a>");
            while (line != "")
            {
                Class.ChapItem item = new Class.ChapItem();
                item.Url = "http://m.blogtruyen.com" + Utils.GetStringBetweenText(line, "\"", "\"");
                item.Title = HttpUtility.HtmlDecode(Utils.GetStringBetweenText(line, "title=\"", "\""));
                item.index = index;
                Chaplist.Add(item);
                index++;
                CheckContent = Utils.GetStringBetweenText(CheckContent,line, "");

                line = Utils.GetStringBetweenText(CheckContent, "<a href=", "/a>");
            }
            listChap.ItemsSource = Chaplist;
            Global.selectedListChap = Chaplist;
        }

        private void listChap_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            Class.ChapItem item = listChap.SelectedItem as Class.ChapItem;
            Global.selectedChap = item;
            if(item!=null)
                NavigationService.Navigate(new Uri("/GUI/PageReader.xaml?Url=" + HttpUtility.UrlEncode(item.Url), UriKind.Relative));
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
           
            var newTile = new StandardTileData()
            {
                Title = title.Text,
                BackgroundImage =new Uri(ImageUrl),
                BackBackgroundImage = new Uri(ImageUrl)  ,
               // Count = 42,

            };
            string navigate = "";
            navigate += "Title=" + Global.selectedItem.Title;
            navigate += "&UrlImage=";
            navigate += "&Url=" + HttpUtility.UrlEncode(Global.selectedItem.Url);
            navigate += "&Description=";

            ShellTile.Create(new Uri("/GUI/ViewDetails.xaml?" + navigate, UriKind.Relative), newTile);
        }
    }
}